package com.gewu.pm.ui.adapter;

import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.gewu.pm.R;
import com.gewu.pm.http.glide.GlideApp;
import com.youth.banner.adapter.BannerAdapter;

import java.util.List;

/**
 * 主要的轮播banner
 */
public class ImageBannerAdapter extends BannerAdapter<String, ImageBannerAdapter.ImageHolder> {
    private Context context;
    private boolean isCircular = false;//轮播图是否是圆角  默认不是
    private final LayoutInflater inflater;

    public ImageBannerAdapter(List<String> mDatas, Context context) {
        //设置数据，也可以调用banner提供的方法,或者自己在adapter中实现
        super(mDatas);
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public ImageBannerAdapter(List<String> mDatas, Context context, boolean isCircular) {
        //设置数据，也可以调用banner提供的方法,或者自己在adapter中实现
        super(mDatas);
        this.context = context;
        this.isCircular = isCircular;
        inflater = LayoutInflater.from(context);
    }

    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    @Override
    public ImageHolder onCreateHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_banner, parent, false);
        return new ImageHolder(view);
    }

    @Override
    public void onBindView(ImageHolder holder, String url, int position, int size) {
        //GlideUtil.loadImageView(context,data.getAdPic(),holder.imageView);
        if (isCircular) {
            // 显示圆角的 ImageView
            GlideApp.with(context)
                    .load(url)
                    .transform(new MultiTransformation<>(new CenterCrop(),
                            new RoundedCorners((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, context.getResources().getDisplayMetrics()))))
                    .into(holder.imageView);
        } else {
            GlideApp.with(context)
                    .load(url)
                    .into(holder.imageView);
        }

    }

    public static class ImageHolder extends RecyclerView.ViewHolder {
        public ImageView imageView;

        public ImageHolder(@NonNull View view) {
            super(view);
            imageView = view.findViewById(R.id.img_banner);
        }
    }
}
